package com.cd2cd.code_template.code_template;

import java.util.Properties;

import javax.annotation.Resource;
import javax.sql.DataSource;

import com.cd2cd.code_template.util.database.MultiDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

@Configuration("codeTemplateDataSourceConfig")
@MapperScan(basePackages = {"com.cd2cd.admin.code_template.code_template.mapper"}, sqlSessionTemplateRef  = "codeTemplateSqlSessionTemplate")
public class CodeTemplateDataSource {

    private static Logger log = LoggerFactory.getLogger(CodeTemplateDataSource.class);

    @Primary
    @ConfigurationProperties(prefix = "datasource.code-template")
	@Bean(name="codeTemplateDataSource")
    @Qualifier("codeTemplateDataSource")
    public DataSource codeTemplateDataSource() {

        log.info("dataSource...");

	    return new MultiDataSource();
    }

    @Primary
    @Bean(name = "codeTemplateSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("codeTemplateDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setTypeAliasesPackage("com.cd2cd.admin.code_template.domain");
        bean.setDataSource(dataSource);
        
        // 设置多数据库支持
        Properties mProperties = new Properties();
        mProperties.put("Oracle", "oracle");
        mProperties.put("MySQL", "mysql");
        mProperties.put("SQL Server", "sqlserver");
        mProperties.put("DB2", "db2");
        
        VendorDatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
        databaseIdProvider.setProperties(mProperties);
        
        bean.setDatabaseIdProvider(databaseIdProvider);
        
        return bean.getObject();
    }

    @Primary
    @Bean(name = "codeTemplateTransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("codeTemplateDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean(name = "codeTemplateSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("codeTemplateSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}	
